package com.ming.multifunction.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ming.multifunction.common.ErrorCode;
import com.ming.multifunction.exception.BusinessException;
import com.ming.multifunction.mapper.CommentMapper;
import com.ming.multifunction.model.domain.Comment;
import com.ming.multifunction.model.domain.Post;
import com.ming.multifunction.model.domain.User;
import com.ming.multifunction.model.vo.CommentsVO;
import com.ming.multifunction.model.vo.UserVO;
import com.ming.multifunction.service.CommentService;


import com.ming.multifunction.service.PostService;
import com.ming.multifunction.service.UserService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
* @author LiSuiMing
* @description 针对表【comment('评论')】的数据库操作Service实现
* @createDate 2024-03-20 17:36:09
*/
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment>
    implements CommentService {

    @Resource
    private PostService postService;

    @Resource
    private UserService userService;

    @Override
    public List<CommentsVO> getComments(long postId) {


        if (postId<=0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"帖子不存在");
        }

        Post post = postService.getById(postId);
        if (post==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"帖子不存在");
        }

        Long userId = post.getUserId();
        if (userId<=0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"用户不存在");
        }
        User user = userService.getById(userId);
        if (user==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"用户不存在");
        }

        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(postId>0,Comment::getPostId,postId);
        List<Comment> commentList = this.list(wrapper);

        if (CollectionUtils.isEmpty(commentList)) {
            return new ArrayList<>();
        }

        ArrayList<CommentsVO> commentsVOList = new ArrayList<>();

        for (Comment comment: commentList){

            Long user_id = comment.getUser_id();
            if (user_id==null){
                continue;
            }

            CommentsVO commentsVO = new CommentsVO();
            BeanUtils.copyProperties(comment,commentsVO);

            User user1 = userService.getById(user_id);
            if (user1!=null){

                UserVO userVO = new UserVO();
                BeanUtils.copyProperties(user1,userVO);
                commentsVO.setUserVO(userVO);
            }

            commentsVOList.add(commentsVO);
        }

        return commentsVOList;
    }

    @Override
    public long saveComment(Comment comment, User loginUser) {

        if (comment==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"评论参数为空");
        }
        if (loginUser==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"用户未登录");
        }
        Long userId = comment.getUser_id();
        if (userId<=0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"用户不存在");
        }

        Long postId = comment.getPostId();
        if (postId<=0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"帖子不存在");
        }

        String content = comment.getContent();
        if (StringUtils.isBlank(content)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"评论内容不能为空");
        }

        boolean save = this.save(comment);
        Long commentId = comment.getId();
        if (!save|| commentId==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"评论失败");
        }

        return commentId;
    }
}




